package com.hxx.day6;

import com.hxx.day5.Person;
import org.junit.Test;

import java.util.*;

public class TestDemo1 {
    public static void main(String[] args) {
        A a = () -> System.out.println("show");
        a.show();
    }

    //含参数
    @Test
    public void test1(){
        AInter1 a = (i,n) -> System.out.println(i+n);
        a.show(1,2);
    }
    //含参数有返回值
    @Test
    public void test2(){
        AInter2 a = (i,n) -> i+n;
        int sum = a.sum(1, 2);
        System.out.println(sum);
    }
    @Test
    public void test3(){
        AInter2 a = (i,n) -> {
            if (i>0){
                return i+n;
            }
           return 0;
        };
        int sum = a.sum(1, 2);
        System.out.println(sum);
    }
    //set集合使用新特性lambda表达式
    @Test
    public void test4(){
        Set<Integer> set = new TreeSet<>(Integer::compareTo);
        set.add(10);
        set.add(2);
        set.add(3);
        set.forEach(i -> System.out.println(i));
    }
    @Test
    public void test5(){
        Set<Integer> set = new TreeSet<>((x,y)->y-x);
        set.add(10);
        set.add(2);
        set.add(3);
        set.forEach(i -> System.out.println(i));
    }
    /**
     * 案例：按照字符串长度排序
     */
    @Test
    public void test6(){
        Set<String> set = new TreeSet<>((x,y)->y.length()-x.length());
        set.add("a");
        set.add("ab");
        set.add("abc");
        set.forEach(System.out::println);
    }
    /**
     * 案例：按照学生年龄排序
     */
    @Test
    public void test7(){
        Set<Student> set = new TreeSet<>((x,y)->x.getAge()-y.getAge());
        set.add(new Student("a",20));
        set.add(new Student("ab",10));
        set.add(new Student("abc",30));
        set.forEach(System.out::println);
    }
    @Test
    public void test8(){
        List<Student> list = List.of(new Student("a", 10),
                new Student("ab", 20),
                new Student("abc", 30));
        //转为流
        list.stream()
                .filter(s->s.getAge()>10)
                .forEach(System.out::println);
    }
    /**
     * 案例：按照年龄排序，如果年龄相同则按照姓名排序
     */
    @Test
    public void test9(){
        //演示数据
        List<Student> list = new ArrayList<>();
        list.add(new Student("leo", 10));
        list.add(new Student("monic", 20));
        list.add(new Student("tom", 30));
        list.add(new Student("alice", 20));

       //排序
        Collections.sort(list,(x,y)->{
            if (x.getAge()-y.getAge()==0) {
                return x.getName().compareTo(y.getName());
            }else{
                return Integer.compare(x.getAge(),y.getAge());
            }
        });
        list.forEach(System.out::println);

    }
}

@FunctionalInterface
interface A{
    void show();
    default void show1(){
        System.out.println("show1");
    }
}

//含参数无返回值
interface AInter1{
    void show(int i,int n);
}
//含参数有返回值
interface AInter2{
    int sum(int i,int n);
}

class Student{
    private String name;
    private int age;
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}























